if exists (select 1
            from  sysobjects
           where  id = object_id('QILU_DBMIGRATION_DETAIL')
            and   type = 'U')
   drop table QILU_DBMIGRATION_DETAIL
;

if exists (select 1
            from  sysobjects
           where  id = object_id('QILU_DBMIGRATION_EXECUTION')
            and   type = 'U')
   drop table QILU_DBMIGRATION_EXECUTION
;

/*==============================================================*/
/* Table: QILU_DBMIGRATION_DETAIL                               */
/*==============================================================*/
create table QILU_DBMIGRATION_DETAIL (
   ID                   varchar(100)         not null,
   EXECUTE_ID           varchar(200)         not null,
   VERSION              varchar(200)         not null,
   STEP_NAME            varchar(200)         not null,
   EXECUTE_TYPE         varchar(20)          not null,
   SCRIPT_FILE          varchar(1000)        null,
   CALLBACK             varchar(1000)        null,
   IGNOR_ERROR          varchar(20)          not null,
   IN_TRANSACTION       varchar(20)          not null,
   STATUS               varchar(20)          not null,
   RESULT_MESSAGE       varchar(1000)        null,
   CREATE_TIME          datetime             not null,
   UPDATE_TIME          datetime             not null,
   constraint PK_QILU_DBMIGRATION_DETAIL primary key nonclustered (ID)
)
;

if exists (select 1 from  sys.extended_properties
           where major_id = object_id('QILU_DBMIGRATION_DETAIL') and minor_id = 0)
begin 
   declare @CurrentUser sysname 
select @CurrentUser = user_name() 
execute sp_dropextendedproperty 'MS_Description',  
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL' 
 
end 


select @CurrentUser = user_name() 
execute sp_addextendedproperty 'MS_Description',  
   '执行明细表', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'ID')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'ID'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '主键',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'ID'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'EXECUTE_ID')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'EXECUTE_ID'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行批次',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'EXECUTE_ID'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'VERSION')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'VERSION'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行版本',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'VERSION'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'STEP_NAME')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'STEP_NAME'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '步骤名称',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'STEP_NAME'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'EXECUTE_TYPE')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'EXECUTE_TYPE'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行类型|DO:升级,UNDO:降级',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'EXECUTE_TYPE'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'SCRIPT_FILE')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'SCRIPT_FILE'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行脚本',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'SCRIPT_FILE'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'CALLBACK')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'CALLBACK'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行回调,类名',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'CALLBACK'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'IGNOR_ERROR')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'IGNOR_ERROR'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '忽略报错|YES:忽略报错,NO:不忽略报错',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'IGNOR_ERROR'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'IN_TRANSACTION')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'IN_TRANSACTION'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '事务',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'IN_TRANSACTION'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'STATUS')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'STATUS'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '状态|SUCCESS:成功,ERROR:失败',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'STATUS'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'RESULT_MESSAGE')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'RESULT_MESSAGE'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '结果消息',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'RESULT_MESSAGE'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'CREATE_TIME')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'CREATE_TIME'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '创建时间',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'CREATE_TIME'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_DETAIL')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'UPDATE_TIME')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'UPDATE_TIME'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '更新时间',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_DETAIL', 'column', 'UPDATE_TIME'
;

/*==============================================================*/
/* Table: QILU_DBMIGRATION_EXECUTION                            */
/*==============================================================*/
create table QILU_DBMIGRATION_EXECUTION (
   ID                   varchar(100)         not null,
   BASE_VERSION         varchar(200)         null,
   TO_VERSION           varchar(200)         not null,
   EXECUTE_ID           varchar(200)         not null,
   EXECUTE_TYPE         varchar(20)          not null,
   STATUS               varchar(20)          not null,
   CREATE_TIME          datetime             not null,
   UPDATE_TIME          datetime             null,
   constraint PK_QILU_DBMIGRATION_EXECUTION primary key nonclustered (ID)
)
;

if exists (select 1 from  sys.extended_properties
           where major_id = object_id('QILU_DBMIGRATION_EXECUTION') and minor_id = 0)
begin 
   declare @CurrentUser sysname 
select @CurrentUser = user_name() 
execute sp_dropextendedproperty 'MS_Description',  
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION' 
 
end 


select @CurrentUser = user_name() 
execute sp_addextendedproperty 'MS_Description',  
   '执行主表', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'ID')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'ID'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '主键',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'ID'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'BASE_VERSION')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'BASE_VERSION'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '基础版本',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'BASE_VERSION'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'TO_VERSION')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'TO_VERSION'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '目标版本',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'TO_VERSION'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'EXECUTE_ID')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'EXECUTE_ID'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行批次',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'EXECUTE_ID'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'EXECUTE_TYPE')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'EXECUTE_TYPE'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '执行类型|DO:升级,UNDO:降级',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'EXECUTE_TYPE'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'STATUS')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'STATUS'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '版本状态|PROCESS:执行中,SUCCESS:正常,ERROR:异常',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'STATUS'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'CREATE_TIME')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'CREATE_TIME'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '创建时间',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'CREATE_TIME'
;

if exists(select 1 from sys.extended_properties p where
      p.major_id = object_id('QILU_DBMIGRATION_EXECUTION')
  and p.minor_id = (select c.column_id from sys.columns c where c.object_id = p.major_id and c.name = 'UPDATE_TIME')
)
begin
   declare @CurrentUser sysname
select @CurrentUser = user_name()
execute sp_dropextendedproperty 'MS_Description', 
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'UPDATE_TIME'

end


select @CurrentUser = user_name()
execute sp_addextendedproperty 'MS_Description', 
   '更新时间',
   'user', @CurrentUser, 'table', 'QILU_DBMIGRATION_EXECUTION', 'column', 'UPDATE_TIME'
;
